<template>
  <div class="shop-container">
    <div class="mui-card" v-for="(item, i) in list" :key="i">
      <div class="mui-card-content">
        <div class="mui-card-content-inner outer">
          <mt-switch
            v-model="selectedObj[item.id]"
            @change="changeSelected(item.id, selectedObj[item.id])"
          ></mt-switch>
          <img :src="images" class="image" />
          <div class="goods-info">
            <h1 class="title">{{ item.title }}</h1>
            <div class="price">
              <span class="left">{{ item.sale }}</span>
              <!-- countObj[item.id] 表示这条商品对应的数量 -->
              <shopcar :initcount="countObj[item.id]" :id="item.id"></shopcar>
              <span><a href="#" @click.prevent="del(item.id)">删除</a></span>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner outer2">
          <div class="left1">
            <p>总计（不含运费）</p>
            <p>
              已勾选商品<span class="danger">{{ selectedCount }}</span
              >件，总价<span class="danger">¥{{ amount }}</span
              >件
            </p>
          </div>
          <mt-button type="danger">去结算</mt-button>
        </div>
      </div>
    </div>
    {{ JSON.stringify(selectedObj) }}
  </div>
</template>

<script>
import phone1 from "../../images/phone1.png";

import shopcar from "../sub-components/shopcar_numbox.vue";

import { mapState, mapGetters, mapMutations } from "vuex";

export default {
  data() {
    return {
      images: [phone1],
      list: [],
    };
  },
  methods: {
    ...mapMutations(["delCart", "changeSelectedState"]),
    del(id) {
      this.list.some((item, i) => {
        if (item.id == id) {
          this.list.splice(i, 1);
          return true;
        }
      });
      this.delCart(id);
    },

    changeSelected(id, select) {
      // 更改开关的状态
      this.changeSelectedState({ id, selected: select });
    },
  },
  components: {
    shopcar: shopcar,
  },
  computed: {
    ...mapGetters([
      "idstr",
      "countObj",
      "selectedObj",
      "selectedCount",
      "amount",
    ]),
    ...mapState(["infos"]),
  },
  created() {
    this.list.push(this.infos);
  },
};
</script>

<style  scoped>
.shop-container {
  background-color: #eee;
  overflow: hidden;
}
.outer {
  display: flex;
  justify-content: space-around;
}
.image {
  width: 60px;
  height: 70px;
}
.goods-info {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  margin-left: 3px;
}
.title {
  padding-left: 10px;
  font-size: 15px;
  color: #000;
}
.price {
  display: flex;
  justify-content: space-around;
  align-items: center;
}
.left {
  color: red;
  font-weight: 700;
  font-size: 15px;
}
.outer2 {
  display: flex;
  justify-content: space-around;
  align-items: center;
}
.danger {
  color: red;
  font-weight: 700;
  font-size: 18px;
}
</style>